exports.get = function(player, req, resp, onHandled) {
    resp.nolog = 1;
    var user = player.user;
    req.args.level = user.status.level;
    req.args.occupying = user.mine.occupying;

    requestWorld(req, resp, function() {
        if( resp.code == 0 ) {
            if( resp.data.gold ) {
                user.status.gold += resp.data.gold;
                player.markDirty('status.gold');

            }

            if( resp.data.lost ) {
                user.mine.occupying = 0;
                player.markDirty('mine.occupying');
            }

            //if( !req.args.id ) {
                resp.data.occupy = user.mine;
            //}

            player.doTask('OccupyMine','all');

            // 返回当日协助次数
            var uAsMine = gAssistMine.getByUid(req.uid);
            resp.data.assist_mine_time = uAsMine.count;

            // 获取好友列表
            var friends = gFriend.getFriends(req.uid);

            // 协助开矿
            var mines = resp.data.mines;
            for( var pos in mines ) {
                var mine = mines[pos];
                var ownerUid = mine.owner;
                if( ownerUid ) {
                    // 好友的矿，返回协助开矿数据
                    if (ownerUid in friends) {
                        var fAsMine = gAssistMine.getByUid(ownerUid);
                        if (uAsMine.count >= +gConfGlobal.AssistMineDailyTimes) {
                            mine.assist_mine = {};
                        } else {
                            mine.assist_mine = clone(fAsMine.local);
                        }
                        mine.assist_mine.name = fAsMine.local_un;
                    }

                    // 自己的矿，返回协助者信息
                    if (ownerUid == player.uid) {
                        var myAsMine = gAssistMine.getByUid(player.uid);
                        mine.assist_mine = clone(myAsMine.local);
                        mine.assist_mine.name = myAsMine.local_un;
                    }
                }
            }
        }
        onHandled();
    });
}

exports.occupy = function(player, req, resp, onHandled) {
    do{
        if( isNaN(req.args.id) || isNaN(req.args.pos) ) {
            resp.code = 1; resp.desc = 'no valid args'; break;
        }

        var user = player.user;
        if( user.mine.occupying ) {
            resp.code = 1; resp.desc = 'occupying'; break;
        }

        var costCash = 0;
        var isCash = req.args.cash ? true : false;
        if( !isCash ) {
            var occupyFreeLimit = gConfGlobal.MineDailyLimit;
            if( user.status.vip ) {
                occupyFreeLimit += gConfVip[user.status.vip].OccupyMineAdd;
            }
            if( user.mine.occupied >= occupyFreeLimit ){
                resp.code = 1; resp.desc = 'occupied reach daily limit'; break;
            }
        }else {
            var confBuy = gConfBuy[user.mine.cash + 1];
            if( !confBuy ) {
                resp.code = 1; resp.desc = 'no more to buy'; break;
            }
                
            costCash = confBuy.CashMine;
            if( user.status.cash < costCash ) {
                resp.code = 1; resp.desc = 'not enough cash'; break;
            }
        }

    } while(false);

    if( resp.code == 1 ) {
        onHandled();
        return;
    }

    var user = player.user;
    req.args.havegold = user.status.gold;
    req.args.level = user.status.level;

    requestWorld(req, resp, function() {
        if( resp.code == 0 ) {
            if( resp.data.gold ) {
                user.status.gold += resp.data.gold;
                player.markDirty('status.gold');
            }

            if( resp.data.occupied ) {
                user.mine.occupying = {level: resp.data.level, id:resp.data.id, pos:resp.data.pos};
                player.doDailyTask(gConfDailyTask.OccupyMine);
                resp.daily_task = gConfDailyTask.OccupyMine;

                gAssistMine.markMine(player, 0, resp.data.id, resp.data.pos, common.getTime());

                if( !isCash ) {
                    user.mine.occupied += 1;
                }else {
                    player.costResource('cash', costCash, user.mine.cash);
                    user.mine.cash += 1;
                } 

                player.markDirty('mine');
                resp.data.cash = -costCash;
            }else{
                resp.data.cash = 0;
            }
        }

        onHandled();
    });
}

exports.leave = function(player, req, resp, onHandled) {
    var user = player.user;
    if( !user.mine.occupying ) {
        resp.code = 1; resp.desc = 'not occupying';
        onHandled();
        return;
    }

    req.args.occupying = user.mine.occupying;
    req.args.level = user.status.level;
    requestWorld(req, resp, function() {
        if( resp.code == 0 ) {
            if( resp.data.gold ) {
                user.status.gold += resp.data.gold;
                player.markDirty('status.gold');
            }

            player.user.mine.occupying = 0;
            player.markDirty('mine.occupying');

            gAssistMine.unMarkMine(player.uid, 0, null);
        }

        onHandled();
    });
}
